package com.service.system;

import com.common.ErrorCodeEnum;
import com.common.emums.DataStatus;
import com.common.excel.ExcelUtil;
import com.common.exception.BusinessException;
import com.common.utils.DateUtils;
import com.common.utils.StringUtils;
import com.core.springSecurity.common.SecurityUtils;

import com.dao.secondary.system.SimInfoMapper;
import com.model.pojo.device.DeviceObdBaseInfo;
import com.model.pojo.device.DeviceObdExFactoryBatchInfo;
import com.model.pojo.device.DevicePcbBaseInfo;
import com.model.pojo.device.DevicePmdBaseInfo;
import com.model.pojo.system.SimInfo;
import com.service.device.DeviceObdBaseInfoService;
import com.service.device.DeviceObdExFactoryBatchInfoService;
import com.service.device.DevicePcbBaseInfoService;
import com.service.device.DevicePmdBaseInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.github.pagehelper.PageHelper;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.*;
import java.util.ArrayList;
import java.util.List;



/**
 * @Author: zql
 * @Description: 【请填写功能名称】服务
 * @Data: 2023-10-08
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SimInfoService {
    private final SimInfoMapper simInfoMapper;
    private final DeviceObdBaseInfoService deviceObdBaseInfoService;
    private final DevicePcbBaseInfoService devicePcbBaseInfoService;
    private final DevicePmdBaseInfoService devicePmdBaseInfoService;
    private final DeviceObdExFactoryBatchInfoService deviceObdExFactoryBatchInfoService;
    public SimInfo getByImei(String imei){
        return simInfoMapper.selectByImei(imei);
    }

    public void excelSimInfo() {
        String filePath = "d:/test/sim2024";
        String fileName = "_04.xlsx";
        File file = new File(filePath+fileName);
//        Long filelength = file.length(); // 获取文件长度
//        byte[] filecontent = new byte[filelength.intValue()];
        try {
            FileInputStream in =new FileInputStream(file);
            List<List<Object>> list = ExcelUtil.getBankListByExcel(in,fileName);
            System.out.println("====list:"+ list.size());
            //表头测试数据
            List<Object> headList = new ArrayList<>();
            if(list.size()>0){
                headList = list.get(0);
            }
            headList.add("设备号");
            headList.add("买卡批次");
            headList.add("状态");
            headList.add("厂家");
            headList.add("出厂日期");
            headList.add("服务年限");
            headList.add("终端类型");
            //创建sheet页
//            setSheet("WorkSheet");
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("WorkSheet");
            //设置表头
//            createHead(headList);
            //创建表头，也就是第一行
            XSSFRow row = sheet.createRow(0);
            for (int i = 0; i < headList.size(); i++) {
                XSSFCell cell = row.createCell(i);
                cell.setCellValue(headList.get(i).toString());
            }

            List<List<Object>> resultList = new ArrayList<>();
            if(list.size()>1) {
                for (int i=1;i<list.size();i++){
                    List<Object> oList = list.get(i);
                    String dtu = "";
                    String batchNo = "";//买卡批次
                    String status = "";//状态
                    String factory = "";//厂家
                    String exDate = "";//出厂日期
                    String serviceYear = "";//服务年限
                    String deviceType = "";//终端类型
                    String imei = oList.get(1).toString();
                    SimInfo simInfo = simInfoMapper.selectByImei(imei);
                    if(simInfo!=null)batchNo = simInfo.getBatch();//购买批次
                    boolean flag = false;//是否找到对应的设备
                    List<DeviceObdBaseInfo> obdBaseList = deviceObdBaseInfoService.getBySim(imei);
                    if(obdBaseList!=null && obdBaseList.size()>0){
                        flag = true;
                        deviceType = "OBD";
                        if(obdBaseList.size()==1){
                            DeviceObdBaseInfo baseInfo = obdBaseList.get(0);
                            dtu = baseInfo.getDtu();
                            if(baseInfo.getBatchId()==null){
                                status = "未出厂";
                            }else if(baseInfo.getBatchId()>0){
                                DeviceObdExFactoryBatchInfo batchInfo = deviceObdExFactoryBatchInfoService.getById(baseInfo.getBatchId());
                                if(batchInfo.getExFactory() ==1){
                                    status = "已出厂";
                                    exDate = DateUtils.parseDateToStr(batchInfo.getExDate(),DateUtils.DATE_FORMAT_YYYY_MM_DD);
                                }
                                factory = batchInfo.getFactoryName();
                                serviceYear = batchInfo.getServiceYear()+"";
                            }
                        }else if(obdBaseList.size()>1){
                            DeviceObdBaseInfo baseInfo = null;
                            for(DeviceObdBaseInfo info :obdBaseList){
                                if(info.getBatchId()!=null &&info.getBatchId()>0){
                                    baseInfo = info;
                                    break;
                                }
                            }
                            if(baseInfo==null)baseInfo = obdBaseList.get(0);
                            dtu = baseInfo.getDtu();
                            if(baseInfo.getBatchId()==null){
                                status = "未出厂";
                            }else if(baseInfo.getBatchId()>0){
                                DeviceObdExFactoryBatchInfo batchInfo = deviceObdExFactoryBatchInfoService.getById(baseInfo.getBatchId());
                                if(batchInfo.getExFactory() ==1){
                                    status = "已出厂";
                                    exDate = DateUtils.parseDateToStr(batchInfo.getExDate(),DateUtils.DATE_FORMAT_YYYY_MM_DD);
                                }
                                factory = batchInfo.getFactoryName();
                                serviceYear = batchInfo.getServiceYear()+"";
                            }
                        }
                    }
                    if(!flag){
                        List<DevicePcbBaseInfo> pcbList = devicePcbBaseInfoService.getBySim(imei);
                        if(pcbList!=null && pcbList.size()>0){
                            flag = true;
                            deviceType = "安卓版";
//                            DevicePcbBaseInfo pcbBaseInfo = pcbList.get(0);
                        }
                    }
                    if(!flag){
                        List<DevicePmdBaseInfo> pmdList = devicePmdBaseInfoService.getBySim(imei);
                        if(pmdList!=null && pmdList.size()>0){
                            flag = true;
                            deviceType = "播种机";
//                            DevicePcbBaseInfo pcbBaseInfo = pcbList.get(0);
                        }
                    }
                    if(!flag){
                        status = "未使用";
                    }
                    oList.add(dtu);
                    oList.add(batchNo);
                    oList.add(status);
                    oList.add(factory);
                    oList.add(exDate);
                    oList.add(serviceYear);
                    oList.add(deviceType);
                    resultList.add(oList);
                }
            }
            //设置内容
//            createContent(resultList);
            //创建表内容，从第二行开始
            for (int i = 0; i < resultList.size(); i++) {
                row = sheet.createRow(i + 1);
                for (int j = 0; j < resultList.get(i).size(); j++) {
                    row.createCell(j).setCellValue(resultList.get(i).get(j).toString());
                }
            }
//            writeToFile("D:/test/sim_test.xlsx");
            file = new File("D:/test/sim_2024"+fileName);
            //将文件保存到指定的位置
            try {
                workbook.write(new FileOutputStream(file));
                System.out.println("写入成功");
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch(FileNotFoundException e) {
            e.printStackTrace();
        } catch(IOException e) {
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
